﻿namespace GeoJsonDemo
{
    #region Imports

    using System;
    using System.Collections;
    using System.IO;
    using System.Linq;
    using Jayrock.GeoJson;
    using Jayrock.Json;

    #endregion

    static class Program
    {
        static void Main()
        {
            var codeBasePath = new Uri(typeof (Program).Assembly.CodeBase).LocalPath;
            var baseDirectoryPath = Path.GetDirectoryName(codeBasePath);
            
            var dataPath = Path.Combine(baseDirectoryPath, "data.txt");            
            var json = File.ReadAllText(dataPath);
            var features = GeoJsonObject.FromJson(json);

            var items = from f in features.Items
                        let geometry = f.FeatureGeometry
                        let type = geometry.ObjectType
                        select new
                        {
                            Id       = f.FeatureId,
                            Name     = f.Properties["name"],
                            Geometry = type,
                            Polygons = type == GeoJsonObjectType.Polygon
                                     ? 1 
                                     : type == GeoJsonObjectType.MultiPolygon 
                                     ? ((IEnumerable) geometry.Coordinates).Cast<object>().Count() 
                                     : (int?) null,
                        };

            foreach (var item in items)
                Console.WriteLine("{2} | {3,2:N0} | {1} ({0}) ", item.Id, item.Name, item.Geometry, item.Polygons);

            var writer = new JsonTextWriter(Console.Out) { PrettyPrint = true };
            features.ToJson(writer);
            writer.Flush();
            Console.WriteLine();
        }
    }
}
